import Vue from 'vue'
// 使用插件
import VueI18n from 'vue-i18n'
Vue.use(VueI18n);
import ElementUILocale from 'element-ui/lib/locale'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import twLocale from 'element-ui/lib/locale/lang/zh-TW'

const DEFAULT_LANG = 'zh' // 默认语言为英文
const LOCALE_KEY = 'CurLanguage'

const locales = { // 引入zh.json以及en.json
  zh: require('./language/local_zh'),
  tw: require('./language/local_zhtw'),
  en: require('./language/local_en')
}

const i18n = new VueI18n({ // 创建带有选项的 VueI18n 实例
  locale: DEFAULT_LANG, // 语言标识，在这里默认为en,即为英文
  messages: locales // 语言包，上边创建的json文件
})

const UIlocales = {
  zh: zhLocale,
  en: enLocale,
  tw: twLocale,
}

// 通过判断lang语言标志符来return先对应的语言
const setUIlocales = lang => {
  switch (lang) {
    case 'zh': return UIlocales.zh
    case 'en': return UIlocales.en
    case 'tw': return UIlocales.tw
  }
}

export const setup = lang => { //切换语言的函数，lang为语言标识，en或者zh
  if (lang == undefined) {
    lang = window.localStorage.getItem(LOCALE_KEY)
    if (locales[lang] == undefined) {
      lang = DEFAULT_LANG
    }
  }
  window.localStorage.setItem(LOCALE_KEY, lang)

  Vue.config.lang = lang
  i18n.locale = lang

  ElementUILocale.use(setUIlocales(lang))

  // for (let key in locales.zh.common) {
  //   console.info(key)
  // }
  // for (let key in locales.zh.basic) {
  //   console.info(key)
  // }
  // for (let key in locales.zh.recipe) {
  //   console.info(key)
  // }
  // for (let key in locales.zh.system) {
  //   console.info(key)
  // }

  // for (let key in locales.zh.run) {
  //   console.info(key)
  // }
  // for (let key in locales.zh.menu) {
  //   console.info(key)
  // }
  // for (let key in locales.en.dialog) {
  // console.info(locales.en.dialog[key])
  // console.info(key)
  // }
}

export const getCurLange = lang => {
  if (lang == undefined) {
    lang = window.localStorage.getItem(LOCALE_KEY)
    if (locales[lang] == undefined) {
      lang = DEFAULT_LANG
    }
  }
  if ('en' == lang) return 'English'
  else if ('zh' == lang) return '中文简体'
  else if ('tw' == lang) return '中文繁體'

}

setup()

export default i18n
